﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

using Gift.DAL;

namespace WebSite.admin.user
{
    public partial class AdminList : BaseAdmin
    {
        static readonly DALAdminLogin theAdmin = new DALAdminLogin();
        DALInstitution theInstitution = new DALInstitution();

        // Methods
        private void bind()
        {
            if (Request.QueryString["action"] == "del")
            {
                if(!string.IsNullOrEmpty( Request.QueryString["uid"]))
                {
                  theAdmin.Delete(int.Parse( Request.QueryString["uid"]));
                }

            }

            int iInstitutionId = 0;
            bool isSuper = false;
            if (LoginAdminUser.IsSuper)
            {
                isSuper = this.cbxIsSuper.Checked;
                if (string.IsNullOrEmpty(this.ddlInstitution.SelectedItem.Value))
                {
                    iInstitutionId = 0;
                }
                else
                {
                    iInstitutionId = int.Parse(this.ddlInstitution.SelectedItem.Value);
                }
            }
            else
            {
                iInstitutionId = LoginAdminUser.InstitutionId;
                isSuper = false;
            }

            DataSet ds = theAdmin.GetListByInstitutionID(iInstitutionId, isSuper);
            if (ds != null && ds.Tables.Count > 0)
            {
                this.GridView1.DataSource = ds;
                this.GridView1.DataBind();

                this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
                this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
                this.LblRecordCount.Text = "总计 " + ds.Tables[0].Rows.Count.ToString() + "个记录";
                if (ds.Tables[0].Rows.Count == 0)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                    this.LblCurrentIndex.Visible = false;
                    this.LblPageCount.Visible = false;
                    this.LblRecordCount.Visible = false;
                }
                else if (this.GridView1.PageCount == 1)
                {
                    this.btnFirst.Visible = false;
                    this.btnPrev.Visible = false;
                    this.btnNext.Visible = false;
                    this.btnLast.Visible = false;
                }
                this.btnFirst.CommandName = "1";
                this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
                this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
                this.btnLast.CommandName = this.GridView1.PageCount.ToString();
                this.ddlCurrentPage.Items.Clear();
                if (this.GridView1.PageCount > 0)
                {
                    for (int i = 1; i <= this.GridView1.PageCount; i++)
                    {
                        this.ddlCurrentPage.Items.Add(i.ToString());
                    }
                    this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    theAdmin.Delete(int.Parse(this.GridView1.DataKeys[i].Value.ToString()));

                }
            }
            base.Response.Write("<script language=javascript>alert('删除成功!');window.location.href='AdminList.aspx';</script>");
            base.Response.End();
        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.bind();
        }
        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.bind();
        }


        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                Label label = (Label)e.Row.FindControl("labelt");
                if ((box != null) && (label != null))
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        if (label.Text.ToString() == "1")
                        {
                            box.Visible = false;
                        }
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                    }
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                if (LoginAdminUser == null || !LoginAdminUser.IsSuper)
                {
                    base.ValidateLoginRule(Gift.Model.ENUM.ERule.AdminList);
                }
                this.BindInstitution();
                this.bind();
            }
        }

        void BindInstitution()
        {
            if (LoginAdminUser.IsSuper)
            {
                DataSet ds;
                ds = theInstitution.GetList(" 1= 1 and IsEnable = 1");
                this.ddlInstitution.DataTextField = "Name";
                this.ddlInstitution.DataValueField = "Id";
                this.ddlInstitution.DataSource = ds;
                this.ddlInstitution.DataBind();
                this.ddlInstitution.Items.Insert(0, new ListItem("所属公司", ""));
            }
            else
            {
                this.ddlInstitution.Visible = false;
            }

        } 


    }
}